home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / X11R4 / cmds / X / os / sprite.X11R3 / connect.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-10-24  |  11.4 KB  |  404 lines

  1. /*-
  2.  * connect.c --
  3.  *    Functions to handle generic connections and their I/O.
  4.  *
  5.  * Copyright (c) 1987 by the Regents of the University of California
  6.  *
  7.  * Permission to use, copy, modify, and distribute this
  8.  * software and its documentation for any purpose and without
  9.  * fee is hereby granted, provided that the above copyright
  10.  * notice appear in all copies.  The University of California
  11.  * makes no representations about the suitability of this
  12.  * software for any purpose.  It is provided "as is" without
  13.  * express or implied warranty.
  14.  *
  15.  *
  16.  */
  17. #ifndef lint
  18. static char rcsid[] =
  19. "$Header: /mic/X11R3/src/cmds/Xsp/os/sprite/RCS/connect.c,v 1.20 89/10/23 18:21:54 tve Exp $ SPRITE (Berkeley)";
  20. #endif lint
  21.  
  22. #include    "Xproto.h"
  23. #include    "spriteos.h"
  24. #include    "dix.h"
  25. #include    "osstruct.h"
  26.  
  27. #include    <bit.h>
  28. #include    <signal.h>
  29. #include    <stdio.h>
  30. #include    <string.h>
  31.  
  32. /*-
  33.  *-----------------------------------------------------------------------
  34.  * CreateWellKnownSockets --
  35.  *    Create listening points via which we can accept connections from
  36.  *    clients. It also finishes off the initialization needed by the
  37.  *    OS layer.
  38.  *
  39.  * Results:
  40.  *    None.
  41.  *
  42.  * Side Effects:
  43.  *    PseudoDevice is set to the streamID of the server's pseudo device.
  44.  *    All the select masks are allocated and initialized.
  45.  *    NumActiveStreams is initialized. Vectors interrupts to HangUp.
  46.  *
  47.  *-----------------------------------------------------------------------
  48.  */
  49. void
  50. CreateWellKnownSockets()
  51. {
  52.     char          errorfile[50];    /* Path to error file */
  53.     int              whichbyte;        /* Used to figure out whether this machine
  54.                      * is LSB or MSB */
  55.     void      HangUp();
  56.     int              Reset();
  57.     struct      sigvec irq;
  58.     int              oldPermMask;        /* Previous permission mask */
  59.     char          hostname[64];
  60.     char          *cp;
  61.  
  62.     gethostname(hostname, sizeof(hostname));
  63.     cp = index(hostname, '.');
  64.     if (cp != (char *)NULL) {
  65.     *cp = '\0';
  66.     }
  67.  
  68.     oldPermMask = umask(0);
  69.  
  70.     sprintf (errorfile, "/tmp/%s:%s.X11R3", hostname, display);
  71.     fclose (stderr);
  72.     fopen (errorfile, "w+");        /* Will go to stderr. */
  73.  
  74.     (void) umask(oldPermMask);
  75.  
  76.  
  77. #ifdef TCPCONN
  78.     TCP_Init();
  79.     NumActiveStreams = max(NumActiveStreams, TCP_Conn+1);
  80. #endif TCPCONN
  81.  
  82.     Pdev_Init(hostname);
  83.     NumActiveStreams = max(NumActiveStreams, Pdev_Conn+1);
  84.     
  85.     /*
  86.      * Allocate all the global bit masks. I'd prefer to do such things
  87.      * in OsInit, but they leave me little choice, since this function
  88.      * is called after OsInit...
  89.      */
  90.     Bit_Alloc (NumActiveStreams, AllStreamsMask);
  91.     Bit_Alloc (NumActiveStreams, LastSelectMask);
  92.     Bit_Alloc (NumActiveStreams, EnabledDevicesMask);
  93.     Bit_Alloc (NumActiveStreams, ClientsWithInputMask);
  94.     Bit_Alloc (NumActiveStreams, AllClientsMask);
  95.     Bit_Alloc (NumActiveStreams, SavedAllClientsMask);
  96.     Bit_Alloc (NumActiveStreams, SavedAllStreamsMask);
  97.  
  98. #ifdef TCPCONN
  99.     Bit_Set (TCP_Conn, AllStreamsMask);
  100. #endif TCPCONN
  101.     Bit_Set (Pdev_Conn, AllStreamsMask);
  102.  
  103.     whichbyte = 1;
  104.  
  105.     if (*(char *)&whichbyte) {
  106.     whichByteIsFirst = 'l';
  107.     } else {
  108.     whichByteIsFirst = 'B';
  109.     }
  110.  
  111.     irq.sv_handler = /*(int (*)())*/ HangUp;
  112.     irq.sv_mask = 0;
  113.     irq.sv_onstack = 0;
  114.     sigvec(SIGINT, &irq, (struct sigvec *) 0);
  115.     sigvec(SIGTERM, &irq, (struct sigvec *) 0);
  116. }
  117.  
  118.     
  119. /*-
  120.  *-----------------------------------------------------------------------
  121.  * CloseDownConnection --
  122.  *    Some client is being booted. Free up its connection resources.
  123.  *
  124.  * Results:
  125.  *    None.
  126.  *
  127.  * Side Effects:
  128.  *    The pseudo-device streams are closed and the private data marked
  129.  *    inactive. The stream is removed from these masks:
  130.  *        AllStreamsMask, SavedAllStreamsMask, AllClientsMask,
  131.  *        SavedAllClientsMask, ClientsWithInputMask
  132.  *
  133.  *-----------------------------------------------------------------------
  134.  */
  135. CloseDownConnection (client)
  136.     ClientPtr    client;        /* Client whose connection should
  137.                  * be closed */
  138. {
  139.     register ClntPrivPtr    pPriv;
  140.     register int          i;
  141.  
  142.     pPriv = (ClntPrivPtr) client->osPrivate;
  143.  
  144.     if (DBG(CONN)) {
  145.     ErrorF ("CloseDownConnection: client %d being booted\n", client->index);
  146.     }
  147.  
  148.     (* pPriv->closeProc) (pPriv);
  149.  
  150.     Bit_Free (pPriv->mask);
  151.     Bit_Free (pPriv->ready);
  152.     free ((char *) pPriv);
  153. }
  154.  
  155. /*-
  156.  *-----------------------------------------------------------------------
  157.  * AddEnabledDevice --
  158.  *    Add another device to keep track of.
  159.  *
  160.  * Results:
  161.  *    None.
  162.  *
  163.  * Side Effects:
  164.  *    The select masks may be expanded. EnabledDevicesMask will be
  165.  *    altered.
  166.  *
  167.  *-----------------------------------------------------------------------
  168.  */
  169. AddEnabledDevice (streamID)
  170.     int        streamID;
  171. {
  172.     ExpandMasks (0, streamID);
  173.  
  174.     Bit_Set (streamID, AllStreamsMask);
  175.     Bit_Set (streamID, SavedAllStreamsMask);
  176.     Bit_Set (streamID, EnabledDevicesMask);
  177. }
  178.  
  179. /*-
  180.  *-----------------------------------------------------------------------
  181.  * RemoveEnabledDevice --
  182.  *    Stop paying attention to some device.
  183.  *
  184.  * Results:
  185.  *
  186.  * Side Effects:
  187.  *
  188.  *-----------------------------------------------------------------------
  189.  */
  190. RemoveEnabledDevice (streamID)
  191.     int        streamID;
  192. {
  193.     Bit_Clear (streamID, AllStreamsMask);
  194.     Bit_Clear (streamID, SavedAllStreamsMask);
  195.     Bit_Clear (streamID, EnabledDevicesMask);
  196. }
  197.  
  198. /*-
  199.  *-----------------------------------------------------------------------
  200.  * OnlyListenToOneClient --
  201.  *    Only pay attention to requests from one client. We continue to
  202.  *    accept new connections, of course, but only take protocol requests
  203.  *    from this single connection.
  204.  *    Once this is done, GrabDone will be set TRUE and and changes to
  205.  *    AllStreamsMask and AllClientsMask should be done to their Saved
  206.  *    copies.
  207.  *    Devices are unaffected, of course.
  208.  *
  209.  * Results:
  210.  *    None.
  211.  *
  212.  * Side Effects:
  213.  *    AllStreamsMask and AllClientsMask are copied into SavedAllStreamsMask
  214.  *    and SavedAllClientsMask, respectively, and have all the other
  215.  *    client bits removed from them.
  216.  *    ClientsWithInputMask has all other clients's bits removed from it.
  217.  *    GrabDone is set TRUE.
  218.  *
  219.  *-----------------------------------------------------------------------
  220.  */
  221. OnlyListenToOneClient (client)
  222.     ClientPtr        client;
  223. {
  224.     ClntPrivPtr      pPriv = (ClntPrivPtr) client->osPrivate;
  225.     int              *tempMask;
  226.  
  227.     /*
  228.      * First take everyone but the given client out of the ClientsWithInputMask
  229.      * We have to allocate a new mask b/c there's no guarantee that the
  230.      * private mask will be as wide as the regular masks, though it
  231.      * usually is.
  232.      */
  233.     Bit_Alloc (NumActiveStreams, tempMask);
  234.     (void) Bit_Copy (pPriv->maskWidth, pPriv->mask, tempMask);
  235.     (void) Bit_Intersect (NumActiveStreams, tempMask,
  236.               ClientsWithInputMask, ClientsWithInputMask);
  237.  
  238.     if (DBG(CONN)) {
  239.     ErrorF("Server grabbed by client %d\n", client->index);
  240.     }
  241.     if (!GrabDone) {
  242.     /*
  243.      * If a grab has not already been done, duplicate the AllStreams
  244.      * and AllClients masks.
  245.      */
  246.       
  247.     Bit_Copy (NumActiveStreams, AllStreamsMask, SavedAllStreamsMask);
  248.     Bit_Copy (NumActiveStreams, AllClientsMask, SavedAllClientsMask);
  249.  
  250.     }
  251.     (void) Bit_Union (NumActiveStreams, tempMask, EnabledDevicesMask,
  252.               AllStreamsMask);
  253.     Bit_Copy (NumActiveStreams, pPriv->mask, AllClientsMask);
  254.     Bit_Free (tempMask);
  255.  
  256.     GrabDone = TRUE;
  257.     grabbingClient = client;
  258. }
  259.     
  260. /*-
  261.  *-----------------------------------------------------------------------
  262.  * ListenToAllClients --
  263.  *    Undo a grab: start paying attention to cries from other clients.
  264.  *
  265.  * Results:
  266.  *    None.
  267.  *
  268.  * Side Effects:
  269.  *    GrabDone is set FALSE and the AllClients and AllStreams masks are
  270.  *    restored from their Saved counterparts.
  271.  *
  272.  *-----------------------------------------------------------------------
  273.  */
  274. ListenToAllClients()
  275. {
  276.     if (GrabDone) {
  277.     if (DBG(CONN)) {
  278.         ErrorF ("Server ungrabbed again\n");
  279.     }
  280.     Bit_Copy (NumActiveStreams, SavedAllStreamsMask, AllStreamsMask);
  281.     Bit_Copy (NumActiveStreams, SavedAllClientsMask, AllClientsMask);
  282.     GrabDone = FALSE;
  283.     }
  284. }
  285.  
  286. /*-
  287.  *-----------------------------------------------------------------------
  288.  * ReadRequestFromClient --
  289.  *    Read a single request from a client. This is just a front-end that
  290.  *    passes the buck to the connection-specific function...
  291.  *
  292.  * Results:
  293.  *    What he said.
  294.  *
  295.  * Side Effects:
  296.  *    Not here..
  297.  *
  298.  *-----------------------------------------------------------------------
  299.  */
  300. char *
  301. ReadRequestFromClient (client, pStatus, oldbuf)
  302.     ClientPtr          client;        /* Client with input */
  303.     int                  *pStatus;   /* Result of read:
  304.                      * >0 -- number of bytes in the request
  305.                      * 0 -- not all the request is there
  306.                      * <0 -- indicates an error */
  307.     char              *oldbuf;    /* The previous buffer (IGNORED) */
  308. {
  309.     ClntPrivPtr          pPriv;        /* Private data for the client */
  310.  
  311.     pPriv = (ClntPrivPtr) client->osPrivate;
  312.  
  313.     oldbuf = (* pPriv->readProc) (pPriv, pStatus, oldbuf);
  314.     if (*pStatus > 0) {
  315.     SchedPacket(client);
  316.     }
  317.     return (oldbuf);
  318. }
  319.  
  320. /*-
  321.  *-----------------------------------------------------------------------
  322.  * WriteToClient --
  323.  *    Function to write stuff to a client. Just a front-end for the
  324.  *    connection-specific stuff.
  325.  *
  326.  * Results:
  327.  *    What he said.
  328.  *
  329.  * Side Effects:
  330.  *    Elsewhere.
  331.  *
  332.  *-----------------------------------------------------------------------
  333.  */
  334. int
  335. WriteToClient (client, numBytes, buf)
  336.     ClientPtr          client;        /* Client to receive the data */
  337.     int                  numBytes;   /* Number of bytes to transmit */
  338.     char              *buf;        /* Buffer to send */
  339. {
  340.     ClntPrivPtr          pPriv;        /* OS-private data */
  341.  
  342.     pPriv = (ClntPrivPtr) client->osPrivate;
  343.  
  344.     return ((* pPriv->writeProc) (pPriv, numBytes, buf));
  345. }
  346.  
  347. /*-
  348.  *-----------------------------------------------------------------------
  349.  * ExpandMasks --
  350.  *    Given a newly-opened stream, expand all the thousands of
  351.  *    select masks to encompass that stream. This includes the private
  352.  *    mask for the client.
  353.  *
  354.  * Results:
  355.  *    None.
  356.  *
  357.  * Side Effects:
  358.  *    The masks will be moved and the old ones freed..
  359.  *
  360.  *-----------------------------------------------------------------------
  361.  */
  362. void
  363. ExpandMasks (pPriv, newStream)
  364.     ClntPrivPtr      pPriv;        /* Private data for client.
  365.                  * Should be 0 if it wasn't opened for a client
  366.                  * (e.g. it's for a device) */
  367.     int              newStream;    /* The new stream's ID number */
  368. {
  369.     if (newStream >= NumActiveStreams) {
  370.     register int newNumStreams = newStream + 1;
  371.     
  372.     AllStreamsMask =        Bit_Expand (newNumStreams, NumActiveStreams,
  373.                         AllStreamsMask);
  374.     LastSelectMask =        Bit_Expand (newNumStreams, NumActiveStreams,
  375.                         LastSelectMask);
  376.     EnabledDevicesMask =     Bit_Expand (newNumStreams, NumActiveStreams,
  377.                         EnabledDevicesMask);
  378.     ClientsWithInputMask =     Bit_Expand (newNumStreams, NumActiveStreams,
  379.                         ClientsWithInputMask);
  380.     AllClientsMask =        Bit_Expand (newNumStreams, NumActiveStreams,
  381.                         AllClientsMask);
  382.     SavedAllClientsMask =    Bit_Expand (newNumStreams, NumActiveStreams,
  383.                         SavedAllClientsMask);
  384.     SavedAllStreamsMask =    Bit_Expand (newNumStreams, NumActiveStreams,
  385.                         SavedAllStreamsMask);
  386.     NumActiveStreams = newNumStreams;
  387.     }
  388.  
  389.     /*
  390.      * We need to also expand the private mask for the client to
  391.      * encompass the new stream.
  392.      */
  393.     if (pPriv) {
  394.     if (newStream >= pPriv->maskWidth) {
  395.         pPriv->mask = Bit_Expand (newStream+1, pPriv->maskWidth,
  396.                       pPriv->mask);
  397.         pPriv->ready = Bit_Expand (newStream+1, pPriv->maskWidth,
  398.                        pPriv->ready);
  399.         pPriv->maskWidth = newStream + 1;
  400.     }
  401.     Bit_Set (newStream, pPriv->mask);
  402.     }
  403. }
  404.